#!/usr/bin/env bash

#get this script real directory
SOURCE="$0"
while [ -h "$SOURCE"  ]; do # resolve $SOURCE until the file is no longer a symlink
    this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    # if $SOURCE was a relative symlink, 
    #we need to resolve it relative to the path 
    #where the symlink file was located
    [[ $SOURCE != /*  ]] && SOURCE="$this_script_dir/$SOURCE"
done
this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"


config_file_name="$this_script_dir/../cfg/remote_host_second.cfg"
expect_ssh_path="$this_script_dir/vlogin_docker.sh"
show_fmt="%4s%15s%5s%6s%15s%5s [%s]\n";

#param:
#   1,username 2,ip 3,passwd 4,id 5,description
print_one_host_info()
{
    printf "$show_fmt" \
        "$1" \
        "$2"  \
        "$3" \
        "$4" \
        "$5" \
        "$6" \
        "$7"; 
}

print_line()
{
    for((print_line_i=0;print_line_i<${1:-80};++print_line_i))
        {
            printf "-";
        }
        printf "\n";
}


###############################script entry##################

if [ ! -f "$config_file_name" ];then
    echo "file [$config_file_name],dose not exist!"
    exit
fi

if [ ! -f "$expect_ssh_path" ];then
    echo "file [$expect_ssh_path],dose not exist!"
    exit
fi

index_of_item=0;

eval `sed -e '/^[ ]*#/d' -e '/^[[:space:]]*$/d' "$config_file_name" | \
    awk 'BEGIN{ FS="!!" ; i='$index_of_item'}
{
    printf("remote_id[%d]=\"%s\"\n\
    docker_id[%d]=\"%s\"\n\
    user_id[%d]='\''%s'\''\n\
    user_name[%d]='\''%s'\''\n\
    password[%d]='\''%s'\''\n\
    port[%d]='\''%s'\''\n\
    description[%d]='\''%s'\''\n",
    i,$1,
    i,$2,
    i,$3,
    i,$4,
    i,$5,
    i,$6,
    i,$7);

    ++i;
}  
END{ }'` 

#trim space
for (( i=0; i < ${#remote_id[*]}; ++i )); do
   remote_id[i]=${remote_id[i]% }
   user_id[i]=${user_id[i]% }
   user_name[i]=${user_name[i]% }
   docker_id[i]=${docker_id[i]% }
   passwd[i]=${passwd[i]% }
   port[i]=${port[i]% }
done

##if has not parameters
if [ "help" == "$1" ]||[ "h" == "$1" ]||[ "pwd" == "$1" ]||[ ! -n "$1" ];then
    num_of_host=${#remote_id[*]}

    printf "$show_fmt" "remid" "ip" "id" "user" "passwd" "port" "description" ;
    print_line
    for((i=0;i<num_of_host;++i));do
        #   1,username 2,ip 3,passwd 4,id 5,description

        [ "$1"x == "pwd"x ] && show_pwd=${password[$i]} || show_pwd=***
        print_one_host_info "${remote_id[$i]}" \
            "${docker_id[$i]}" \
            "${user_id[$i]}" \
            "${user_name[$i]}" \
            "${show_pwd}" \
            "${port[$i]}" \
            "${description[$i]}";

            #"${password[$i]}" \
    done
    exit 0;
fi

#try to login the remote computer by ssh
for (( i=0; i < ${#remote_id[*]}; ++i )); do
    if [ "${user_id[i]}" == "$1" ] ; then
        print_one_host_info "${remote_id[$i]}" \
            "${docker_id[$i]}" \
            "${user_id[$i]}" \
            "${description[$i]}";
        #echo "s ${remote_id[$i]}  "docker exec -ti ${docker_id[$i]} /bin/bash || exit" "source .bash_profile""

        #s ${remote_id[$i]}  "vlogin.sh  ${user_name[i]} ${host_ip[i]} \
        #        ${user_passwd[i]//$/\\$} \
        #        ${ssh_port[i]}" "source .bash_profile"

        #${docker_id[$i]}, represent IP

        [ "$2"x == "pwd"x ] && pwd_str=should_pwd || pwd_str=""

        if [ -n "$2" ] ;then
            s $pwd_str ${remote_id[$i]} \
                "vlogin.sh ${user_name[$i]} ${docker_id[$i]} ${password[$i]} ${port[$i]}" \
                "echo ${user_name[$i]}@${docker_id[$i]} ${password[$i]} ${port[$i]} ${description[$i]} && source .profile && $2 ;" 
        else
            #s $pwd_str ${remote_id[$i]}  "history -wc && vlogin.sh ${user_name[$i]} ${docker_id[$i]} ${password[$i]} ${port[$i]}"  "echo ${user_name[$i]}@${docker_id[$i]} ${password[$i]} ${port[$i]} ${description[$i]} && source .profile" 
            [ "$2"x == "pwd"x ] && show_pwd=${password[$i]} || show_pwd=***
            s $pwd_str ${remote_id[$i]}  "history -wc && vlogin.sh ${user_name[$i]} ${docker_id[$i]} ${password[$i]} ${port[$i]}"  "echo ${user_name[$i]}@${docker_id[$i]} $show_pwd ${port[$i]} ${description[$i]} && source .profile" 
            #${password[$i]}
        fi
        exit 0;
    fi
done
#if do not find id in configure file
printf "id [%s] donot found in configure file,please check!\n" "$1"

exit 0;
